package cn.tedu.apps_system.service.impl;

import cn.tedu.apps_system.base.response.JsonResult;
import cn.tedu.apps_system.base.response.ResultCode;
import cn.tedu.apps_system.base.utils.CacheUtil;
import cn.tedu.apps_system.mapper.GovernmentMapper;
import cn.tedu.apps_system.pojo.dto.PoliciesSetDto;
import cn.tedu.apps_system.pojo.dto.PromotionDto;
import cn.tedu.apps_system.pojo.dto.VideoDto;
import cn.tedu.apps_system.pojo.entity.Policies;
import cn.tedu.apps_system.pojo.entity.Promotion;
import cn.tedu.apps_system.pojo.entity.Video;
import cn.tedu.apps_system.pojo.vo.PoliciesListVO;
import cn.tedu.apps_system.pojo.vo.PromotionVO;
import cn.tedu.apps_system.pojo.vo.VideoVO;
import cn.tedu.apps_system.service.GovernmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GovernmentServiceImpl implements GovernmentService {
    @Autowired
    private GovernmentMapper governmentMapper;

    @Autowired
    private CacheUtil cacheUtil;

    @Override
    public JsonResult setPolicies(PoliciesSetDto policiesSetDto) {
        log.debug("policiesSetDto: "+ policiesSetDto);
        Policies policies = new Policies();
        BeanUtils.copyProperties(policiesSetDto,policies);
        policies.setCreateTime(new Date());
        int n = governmentMapper.set(policies);
        if (n>0){
            return JsonResult.ok();
        }
        return new JsonResult(ResultCode.FAILED);
    }

    @Override
    public JsonResult getList() {
        Serializable policiesIndex=cacheUtil.get("policies_Index");
        if (policiesIndex!=null){
            log.debug("数据来自Redis数据库...");
            return JsonResult.ok(policiesIndex);
        }
        List<PoliciesListVO> list = governmentMapper.getList();
        cacheUtil.setExpire("policies_Index", (Serializable) list,60);
        return JsonResult.ok(list);
    }

    @Override
    public JsonResult addVideo(VideoDto videoDto) {
        log.debug("VideoDto :"+videoDto);
        Video video = new Video();
        BeanUtils.copyProperties(videoDto,video);
        video.setCreateTime(new Date());

        int n = governmentMapper.addVideo(video);
        if (n>0){
            return JsonResult.ok();
        }
        return new JsonResult(ResultCode.FAILED);
    }

    @Override
    public JsonResult getVideo() {
        Serializable video=cacheUtil.get("video_Index");
        if (video!=null){
            log.debug("数据来自Redis数据库...");
            return JsonResult.ok(video);
        }
        List<VideoVO> list = governmentMapper.getVideo();
        cacheUtil.setExpire("video_Index", (Serializable) list,60);
        return JsonResult.ok(list);
    }

    @Override
    public JsonResult addProduct(PromotionDto promotionDto) {
        log.debug("promotionDto: "+promotionDto);
        Promotion promotion = new Promotion();
        BeanUtils.copyProperties(promotionDto,promotion);
        int n = governmentMapper.addProduct(promotion);
        if (n>0){
            return JsonResult.ok();
        }
        return new JsonResult(ResultCode.FAILED);
    }

    @Override
    public JsonResult reviseProduct(PromotionDto promotionDto) {
        log.debug("promotionDto: "+promotionDto);
        Promotion promotion = new Promotion();
        BeanUtils.copyProperties(promotionDto,promotion);
        int n = governmentMapper.reviseProduct(promotion);
        if (n>0){
            return JsonResult.ok();
        }
        return new JsonResult(ResultCode.FAILED);
    }

    @Override
    public JsonResult getProductList() {
        Serializable promotion=cacheUtil.get("promotion_Index");
        if (promotion!=null){
            log.debug("数据来自Redis数据库...");
            return JsonResult.ok(promotion);
        }
        List<PromotionVO> list = governmentMapper.getProductList();
        cacheUtil.setExpire("promotion_Index", (Serializable) list,60);
        return JsonResult.ok(list);
    }

    @Override
    public JsonResult reviewPolicy(Long id) {
        log.debug("开始审核政策，ID: {}", id);

        Policies policy = governmentMapper.getById(id);
        if (policy == null) {
            log.warn("审核失败：政策不存在，ID: {}", id);
            return new JsonResult(ResultCode.POLICY_NOT_EXIST);
        }
//
//        if (policy.getStatus() != 0) {
//            log.warn("审核失败：政策已审核过，ID: {}", id);
//            return new JsonResult(ResultCode.POLICY_ALREADY_REVIEWED);
//        }

        int rows = governmentMapper.updateStatusById(id, 1); // 审核状态为1
        if (rows <= 0) {
            log.error("审核失败：更新状态失败，ID: {}", id);
            return new JsonResult(ResultCode.FAILED);
        }

        log.info("政策审核成功，ID: {}", id);
        return JsonResult.ok("审核成功");
    }

    @Override
    public JsonResult deletePolicy(Long id) {
        log.debug("开始删除政策，ID: {}", id);

        Policies policy = governmentMapper.getById(id);
        if (policy == null) {
            log.warn("删除失败：政策不存在，ID: {}", id);
            return new JsonResult(ResultCode.POLICY_NOT_EXIST);
        }

        int rows = governmentMapper.deleteById(id);
        if (rows <= 0) {
            log.error("删除失败：数据库未删除任何记录，ID: {}", id);
            return new JsonResult(ResultCode.POLICY_DELETE_FAILED);
        }

        log.info("政策删除成功，ID: {}", id);
        return JsonResult.ok("删除成功");
    }


}
